{extend name="base"/}
{block name="resources"}
{/block}
{block name="main"}
<div class="layui-collapse ns-tips">
	<div class="layui-colla-item">
		<h2 class="layui-colla-title">操作提示</h2>
		<ul class="layui-colla-content layui-show">
			<li>店铺套餐管理</li>
		</ul>
	</div>
</div>

<!-- 搜索框 -->
<div class="ns-single-filter-box">
	<button class="layui-btn ns-bg-color" onclick="add()">添加套餐</button>

	<div class="layui-form">
		<div class="layui-input-inline">
			<input type="text" name="search_keys" placeholder="请输入关键词" autocomplete="off" class="layui-input">
			<button type="button" class="layui-btn layui-btn-primary" lay-filter="search" lay-submit>
				<i class="layui-icon">&#xe615;</i>
			</button>
		</div>
	</div>
</div>

<!-- 列表 -->
<table id="group_list" lay-filter="group_list"></table>

<!-- 操作 -->
<script type="text/html" id="operation">
	<div class="ns-table-btn">
		<a class="layui-btn" lay-event="edit">编辑</a>
		<a class="layui-btn" lay-event="delete">删除</a>
	</div>
</script>

<!-- 状态 -->
<script type="text/html" id="status">
	{{ d.status == 1 ? '正常' : '锁定'}}
</script>
{/block}
{block name="script"}
<script>
	var table, form, laytpl, layer_pass, repeat_flag = false; //防重复标识
	layui.use(['form', 'laytpl'], function() {
		form = layui.form;
		laytpl = layui.laytpl;
		form.render();

		table = new Table({
			elem: '#group_list',
			url: ns.url("admin/sitegroup/lists"),
			cols: [
				[{
					field: 'group_name',
					title: '套餐名称',
					width: '20%',
					unresize: 'false'
				},{
					title: '价格',
					width: '15%',
					unresize: 'false',
					templet: function (data) {
						var unit;
						if (data.unit == 'week') {
							unit = '周';
						} else if (data.unit == 'month') {
							unit = '月';
						} else if (data.unit == 'year') {
							unit = '年';
						}
						return data.fee + '元/'+ unit;
					}
				}, {
					title: '上架状态',
					width: '18%',
					unresize: 'false',
					templet: function (data) {
						if (data.status == 1) {
							return '已上架';
						} else {
							return  '已下架';
						}
					}
				}, {
					field: 'create_time',
					title: '创建时间',
					width: '18%',
					unresize: 'false',
					templet: function(data) {
						return ns.time_to_date(data.create_time); //创建时间转换方法
					}
				}, {
					title: '操作',
					width: '16%',
					toolbar: '#operation',
					unresize: 'false'
				}]
			],
		});

		/**
		 * 监听工具栏操作
		 */
		table.tool(function(obj) {
			var data = obj.data;
			switch (obj.event) {
				case 'edit': //编辑
					location.href = ns.url('admin/sitegroup/edit', {group_id: data.group_id});
					break;
				case 'delete': //删除
					deleteGroup(data.group_id);
					break;
			}
		});

		/**
		 * 删除
		 */
		function deleteGroup(group_id) {
			if (repeat_flag) return false;
			repeat_flag = true;

			layer.confirm('确定要删除该套餐吗?', function() {
				$.ajax({
					type: 'POST',
					dataType: 'JSON',
					url: ns.url("admin/sitegroup/delete"),
					data: {group_id: group_id},
					success: function(res) {
						layer.msg(res.message);
						repeat_flag = false;

						if (res.code == 0) {
							table.reload();
						}
					}
				});
			}, function () {
				layer.close();
				repeat_flag = false;
			});
		}
		
		/**
		 * 搜索功能
		 */
		form.on('submit(search)', function(data) {
			table.reload({
				page: {
					curr: 1
				},
				where: data.field
			});
		});

	});
	
	function add() {
		location.href = ns.url("admin/sitegroup/add");
	}

</script>

{/block}